//
//  AFHTTPSessionManager+Config.m
//  WeClubs
//
//  Created by KevinWang on 17/6/14.
//  Copyright © 2017年 Zed. All rights reserved.
//

#import "AFHTTPSessionManager+Config.h"
#import "ZClientInfoCenter.h"
#import "NSString+ZUtilities.h"
#import "ZUserInfo.h"
#import <objc/runtime.h>

static void * Parameter = (void *)@"parameter";

@implementation AFHTTPSessionManager (Config)

- (NSMutableDictionary*)parmars
{
    return objc_getAssociatedObject(self, Parameter);
}

- (void)setParmars:(NSMutableDictionary *)parmars
{
    objc_setAssociatedObject(self, Parameter, parmars, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)configSetting
{
   // 跳过ssl验证
   self.securityPolicy.allowInvalidCertificates=NO;
    //设置请求超时
   self.requestSerializer.timeoutInterval = 15;
}

+ (NSDictionary*)configBaseParmars:(NSDictionary *)parmars
{

    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    
    // 拼接相关参数
    double random = arc4random_uniform(1024) + [ZClientInfoCenter sharedInstance].timeIntervalSince1970;
    NSString *randomString = [NSString stringWithFormat:@"%zd", random];
    //1 id
    [dic setValue:[randomString zMD5HexString] forKey:@"id"];
    
    // 获取客户端相关信息
    ZClientInfoCenter *info = [ZClientInfoCenter sharedInstance];
    
    //2 client
    [dic setValue:@{
                    @"caller": @"ios_test",
                    @"version": info.appVersion,
                    @"date": @(info.timeIntervalSince1970),
                    @"ex": @{
                            @"sc": info.screenSize,
                            @"dv": info.deviceModel,
                            @"uid": @"yytfqmitoljwhwtltbybb",
                            @"sf": kAPPSource,
                            @"os": info.osVersion,
                            }
                    } forKey:@"client"];
    
    //3 encrypt
    [dic setValue:@"md5" forKey:@"encrypt"];
    
    //配置全局参数token跟user_id
    NSMutableDictionary *globParam = [NSMutableDictionary dictionaryWithDictionary:parmars];
    if ([globParam.allKeys containsObject:@"notoken"] && [globParam[@"notoken"] isEqual:@"notoken"] ) {
        [globParam removeObjectForKey:@"notoken"];
    }else{
        [globParam setValue:@([ZUserInfo sharedInstance].userId) forKey:@"user_id"];
        [globParam setValue:[ZUserInfo sharedInstance].token forKey:@"token"];
    }
    
  
    //参数签名
    //4 sign
    [dic setValue:[self encryptData:globParam] forKey:@"sign"];
    
    //5 data
    [dic setValue:globParam forKey:@"data"];
    return dic;
}

// data参数加密处理
+ (NSString *)encryptData:(NSDictionary *)data
{
    // ========== 拼接Data的Sign字符串 ==========
    NSArray *allKeys = [data.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    
    NSMutableString *signDataStr = [NSMutableString string];
    for (NSString *keyStr in allKeys) {
        //Data参数的Key值检测
        BOOL isAssert = [keyStr isKindOfClass:[NSString class]];
        if (isAssert) {
            NSString *errorInfo = [NSString stringWithFormat:@"请求的Data参数中, 包含了非字符串的Key值: %@", keyStr];
            NSAssert(isAssert, errorInfo);
        }
        
        NSString *keyValueStr = [NSString stringWithFormat:@"%@%@", keyStr, [data objectForKey:keyStr]];
        [signDataStr appendString:keyValueStr];
    }
    ZLog(@"SignData: %@", signDataStr);
    
    // ========== 添加secret ==========
    NSString *secretStr = [SECRET_WORD zMD5HexString];
    ZLog(@"Secret: %@", secretStr);
    
    [signDataStr insertString:secretStr atIndex:0];
    [signDataStr appendString:secretStr];
    ZLog(@"SecretSignData: %@", signDataStr);
    
    // ========== SHA1 | MD5 加密 ==========
    NSString *SHA1SignStr = [signDataStr zSHA1String];
    ZLog(@"SHA1Sign: %@", SHA1SignStr);
    NSString *MD5SignStr = [SHA1SignStr zMD5HexString];
    ZLog(@"MD5Sign: %@", MD5SignStr);
    
    return MD5SignStr;
}

@end
